package com.example.anew.journey.mynewjourney.logic.forgotPassword.persenter;

import android.text.TextUtils;

import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.logic.forgotPassword.view.ISForgotPasswordView;
import com.example.anew.journey.mynewjourney.logic.getVersion.bean.GetVersionBean;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司19
 * Created by 20433 on 2018/3/23
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class ForgotPasswordPersenter {
    private ISForgotPasswordView mISForgotPasswordView ;

    public ForgotPasswordPersenter(ISForgotPasswordView miSForgotPasswordView) {
        this.mISForgotPasswordView = miSForgotPasswordView;
    }
    //点击 获取验证码
    public void onClickGetVersionCode(){
        String phoneNumber = mISForgotPasswordView.getInputPhoneNumber();
        if(TextUtils.isEmpty(phoneNumber)) mISForgotPasswordView.showToastInformation("请输入手机号");
        getVersionCode(phoneNumber);

    }

    public void getVersionCode(final String phoneNumber) {
        String url = RBConstants.SERVER_URL+RBConstants.OTHER_GET_VERSION_CODE;
        OkHttpUtils
                .get()
                .url(url)
                .addParams("tel", phoneNumber)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson =   new Gson();
                       // NLogger.e("TAG","getVersionCode==================="+NetworkResponseDate);
                        final GetVersionBean getVersion =  gson.fromJson(NetworkResponseDate,GetVersionBean.class);
                        // TODO 移植
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(getVersion.errorCode == 200){
                                    mISForgotPasswordView.toResetPasswordPager(phoneNumber);
                                    mISForgotPasswordView.showToastInformation("验证码发送成功");
                                }
                                if(getVersion.errorCode == 400)  mISForgotPasswordView.showToastInformation("不能访问");
                                if(getVersion.errorCode == 500)   mISForgotPasswordView.showToastInformation("未访问到服务");
                                else  mISForgotPasswordView.showToastInformation(getVersion.errorMessage);
                            }
                        });
                        return null;
                    }
                    @Override
                    public void onError(Call call, final Exception e, int id) {
//                        MyApplication.getmHandler().post(new Runnable() {
//                            @Override
//                            public void run() {
//                                mISForgotPasswordView.showToastInformation("请查看网络");
//                            }
//                        });


                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }
}
